﻿<?php
require('configs/include.php');

class c_crear_programa extends super_controller {

    public function crear() {
        $programa = new programa($this->post);

        if (is_empty($programa->get('nombre'))) {
            throw_exception("Debe ingresar el nombre del programa antes de continuar");
        }if (is_empty($programa->get('funcionalidad'))) {

            throw_exception("Debe ingresar la funcionalidad del programa  antes de continuar");
        }if (is_empty($programa->get('codigo_fuente'))) {
            throw_exception("Debe ingresar el codigo de fuente del programa  antes de continuar");
        }

        $typeSession = $_SESSION['usuario']['cedula'];
        $programa->set('programador', $typeSession);
        $this->orm->connect();
        $this->orm->insert_data("normal", $programa);
        $this->orm->close();
        $this->type_warning = "success";

        $this->msg_warning = "Se creó correctamente el Programa";
        $this->img_warning = "correct.png";
        $this->engine->assign('img_warning', $this->img_warning);
        $this->engine->assign('type_warning', $this->type_warning);
        $this->engine->assign('msg_warning', $this->msg_warning);
        $this->temp_aux = 'message.tpl';
    }

    public function cargar_kernels() {
        $options['kernel']['lvl2'] = "by_stateF";
        $this->orm->connect();
        $this->orm->read_data(array("kernel"), $options);
        $kernels = $this->orm->get_objects("kernel");
        $this->orm->close();
        $this->engine->assign('kernels', $kernels);
    }

    public function display() {
        $this->cargar_kernels();
        $varAux = $this->gvar['n_index'] . " -> " . $this->gvar['n_program'];
        $this->engine->assign('ubicationPath', $varAux);
        $this->engine->display('header.tpl');
        $this->engine->display($this->temp_aux);
        $this->engine->display('crear_programa.tpl');
        $this->engine->display('footer.tpl');
    }

    public function run() {
        $typeSession = $_SESSION['usuario']['tipo'];
        if ($typeSession == "lider" || $typeSession == "programador" || $typeSession == "intermediario") {
            try {
                if (isset($this->get->option)) {
                    $this->{$this->get->option}();
                }
            } catch (Exception $e) {
                $this->error = 1;
                $this->msg_warning = $e->getMessage();
                $this->img_warning = "incorrect.png";
                $this->engine->assign('type_warning', $this->type_warning);
                $this->engine->assign('msg_warning', $this->msg_warning);
                $this->engine->assign('img_warning', $this->img_warning);
                $this->temp_aux = 'message.tpl';
            }
            $this->display();
        } else {
            $this->engine->display('header.tpl');
            $this->engine->display('sinPermisos.tpl');
            $this->engine->display('footer.tpl');
        }
    }

}

$call = new c_crear_programa();
$call->run();
?>
